package com.taxi.driver.module.order.complain;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.qianxx.utils.TypeUtils;
import com.qianxx.view.HeadView;
import com.taxi.driver.R;
import com.taxi.driver.common.BaseActivity;
import com.taxi.driver.config.DriverType;
import com.taxi.driver.config.IConstants;
import com.taxi.driver.module.order.complain.dagger.DaggerOrderComplainComponent;
import com.taxi.driver.module.order.complain.dagger.OrderComplainModule;
import com.taxi.driver.module.vo.ComplainVO;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 功能描述：
 */
public class OrderComplainActivity extends BaseActivity implements OrderComplainContract.View {

    public static void actionStart(Context context, String orderUuid) {
        Intent intent = new Intent(context, OrderComplainActivity.class);
        intent.putExtra(IConstants.ORDER_UUID, orderUuid);
        context.startActivity(intent);
    }

    @Inject
    OrderComplainPresenter mPresenter;
    OrderComplainAdapter mAdapter;

    @BindView(R.id.head_view)
    HeadView mHeadView;
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;

    TextView mTvTag;
    ViewHolder mHolder;
    boolean mIsComplainSuccess; //是否投诉成功


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_complain);
        ButterKnife.bind(this);
        DaggerOrderComplainComponent.builder().appComponent(getAppComponent())
                .orderComplainModule(new OrderComplainModule(this)).build().inject(this);
        mPresenter.setOrderUuid(getIntent().getStringExtra(IConstants.ORDER_UUID));

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new OrderComplainAdapter(this, R.layout.item_order_complain);
        View topView = LayoutInflater.from(this).inflate(R.layout.layout_tv_view, mRecyclerView, false);
        mTvTag = (TextView) topView.findViewById(R.id.tv_tag);
        if(mPresenter.driverType() == DriverType.SPECIAL){
            mTvTag.setText(R.string.order_complain_reason);
        }
        View bottomView = LayoutInflater.from(this).inflate(R.layout.layout_btn_view, mRecyclerView, false);
        mHolder = new ViewHolder(bottomView);
        mHolder.mTvSubmit.findViewById(R.id.tv_submit).setOnClickListener(v -> {
            mPresenter.submitComplain(mAdapter.getContent()); //提交投诉
        });
        mAdapter.addHeaderView(topView);
        mAdapter.addFooterView(bottomView);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnClickListener(R.id.layout, (position, view, item) -> {
            if (mIsComplainSuccess) return;
            mAdapter.select(position); //选择投诉项，刷新显示
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.subscribe();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPresenter.unsubscribe();
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public void showComplainMsg(List<ComplainVO> vos) {
        if (mIsComplainSuccess) return;
        mAdapter.setAll(vos); //刷新显示
    }

    @Override
    public void complainSuccess(String orderUuid, String content) {
        setComplainDisplay(content, getResources().getString(R.string.order_complain_dealing), true);
    }

    @Override
    public void complained(String orderUuid, String content, String complainResult, boolean isDealing) {
        setComplainDisplay(content, complainResult, isDealing);
    }

    // 设置投诉的显示
    private void setComplainDisplay(String content, String complainResult, boolean isDealing) {
        mIsComplainSuccess = true; //投诉成功
        mTvTag.setText(R.string.order_complain_yours);
        mAdapter.setAll(getSelectList(content));
        mHolder.mTvSubmit.setVisibility(View.GONE);
        visible(mHolder.mTvResultTag, mHolder.mTvResult);
        mHolder.mTvResultTag.setText(isDealing
                ? R.string.order_complain_ing
                : R.string.order_complain_result);
        mHolder.mTvResult.setText(isDealing
                ? getResources().getString(R.string.order_complain_dealing)
                : TypeUtils.getValue(complainResult));
    }

    private List<ComplainVO> getSelectList(String content) {
        List<ComplainVO> list = new ArrayList<>();
        list.add(new ComplainVO(content));
        return list;
    }

    static class ViewHolder {
        @BindView(R.id.tv_submit)
        TextView mTvSubmit;
        @BindView(R.id.tv_result_tag)
        TextView mTvResultTag;
        @BindView(R.id.tv_result)
        TextView mTvResult;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
